package nio.channel.filechannel;


import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author charwayH
 * FileChannel间的数据传输(文件复制)
 */
public class FileChannelDemo3 {
    public static void main(String[] args) throws IOException {
        // 创建两个FileChannel
        RandomAccessFile aFile = new RandomAccessFile("/Users/charwayh/Desktop/java-myself/hello-nio/src/main/java/channel/1.txt", "rw");
        FileChannel fromChannel = aFile.getChannel();

        RandomAccessFile bFile = new RandomAccessFile("/Users/charwayh/Desktop/java-myself/hello-nio/src/main/java/channel/3.txt", "rw");
        FileChannel toChannel = bFile.getChannel();

        // fromChannel中的数据传输到toChannel
        long position = 0;
        long size = fromChannel.size();
        toChannel.transferFrom(fromChannel, position, size);
        // 效果相同（方向不同）
        //fromChannel.transferTo(position, size,toChannel);

        // 关闭
        aFile.close();
        bFile.close();

        System.out.println("复制结束...");
    }
}
